<?php
class resource_display {
	
	private $_hash = null;
	private $_config = null;
	private $_concept = null;
	
	public function __construct($hash, $typeName = 'image') {
		$this->_hash = $hash;
		try {
			$this->_concept = new resource_file_type($typeName);
		} catch (Exception $e) {
			return;
		}
		$class = 'resource_config_' . strtolower($this->_concept->getKey());
		$this->_config = new $class();
	}
		
	public function __call($method, array $args) {
		if(preg_match('/^get(\w+?)$/', $method, $matches)) {
			//默认：true
			$secure = isset($args[0]) ? $args[0] : true;
			return $this->get($matches[1], $secure);
		}
		return null;
	}
	
	public function getHash() {
		return $this->_hash;
	}
	
	/**
	 * @return resource_file_type
	 */
	public function getType() {
		return $this->_concept; 
	}
	
	public function __toString() {
		return $this->get('large');
	}
	
	public function get($name, $secure = true) {
		if(!preg_match('/^[0-9a-f]{32}$/is', $this->_hash)) {
			return !$this->_config ? NULL : $this->_config->noMedia();
		} else {
			$http = !$this->_config ? '' : $this->_config->http($secure);
			$ext = !$this->_config ? '' : $this->_config->findExtension($this->_hash);
			if($secure) {
				return $http . '/' . $this->_hash . '/' . strtolower($name) . $ext;
			} else {
				return $http . $this->_config->md5Path($this->_hash) . '/' . strtolower($name) . $ext;
			}
		}
	}
	
}

